package cn.maihe.elg.operation.web.order;

import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.enums.AcceptOrderStatus;
import cn.maihe.elg.operation.model.enums.ApplyOrderStatus;
import cn.maihe.elg.operation.model.enums.GuaranteeCategory;
import cn.maihe.elg.operation.model.enums.GuaranteeType;
import cn.maihe.elg.operation.model.vo.order.ApplyOrderDetailVO;
import cn.maihe.elg.operation.model.vo.order.ApplyOrderStatusOperateVO;
import cn.maihe.elg.operation.model.vo.order.ListApplyOrderDO;
import cn.maihe.elg.operation.model.vo.order.ListApplyOrderVO;
import cn.maihe.elg.operation.repository.entity.ApplyOrderInfoDO;
import cn.maihe.elg.operation.service.order.ApplyOrderInfoService;
import cn.maihe.elg.operation.service.order.GuaranteeProgressInfoService;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import cn.maihe.elg.operation.utils.DownloadExcelTool;
import cn.maihe.elg.operation.utils.StringUtil;
import cn.maihe.framework.starter.base.sql.PageBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 我的申请出函订单管理
 * @Author WJH
 * @Date 2021/05/06
 */
@Api(value = "我的申请出函订单管理", tags = "我的申请出函订单管理")
@RestController
@RequestMapping("/api")
public class ApplyOrderInfoController {

    @Resource
    private ApplyOrderInfoService applyOrderInfoService;
    @Resource
    private GuaranteeProgressInfoService guaranteeProgressInfoService;
    @Resource
    private ElgResourceService elgResourceService;
    @Resource
    private DownloadExcelTool downloadExcelTool;

    @ApiOperation(value = "分页查询我的申请订单信息", notes = "根据我的申请订单 录入参数 分页查询")
    @RequestMapping(value = "/applyOrders", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "centerOrderId", value = "中心订单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "applyOrderNo", value = "申请订单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "bidCustomerName", value = "申请企业名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sectionName", value = "标段名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "guaranteeType", value = "保函类型", dataType = "int", paramType = "query", allowableValues = "0,1,2,3"),
            @ApiImplicitParam(name = "guaranteeCategory", value = "保函品种", dataType = "int", paramType = "query", allowableValues = "0,1,2,3,4"),
            @ApiImplicitParam(name = "applyOrderStatus", value = "订单状态", dataType = "int", paramType = "query", allowableValues = "0,1,2,3,4,5"),
            @ApiImplicitParam(name = "centerName", value = "交易中心名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "guarantorName", value = "出函机构名称", dataType = "String", paramType = "query"),

    })
    public PageBo<ListApplyOrderDO> pageList(int pageNo, int pageSize, ListApplyOrderVO vo) {
        return applyOrderInfoService.selectPageBySql(pageNo, pageSize, vo);
    }

    @ApiOperation(value = "获取我的申请订单信息", notes = "根据我的申请订单信息id获取我的申请订单信息")
    @ApiImplicitParam(name = "id", value = "我的申请订单信息id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/applyOrder/{id}", method = RequestMethod.GET)
    public ApplyOrderDetailVO getApplyOrderDetailById(@PathVariable Long id) {
        ApplyOrderDetailVO curVO = applyOrderInfoService.getApplyOrderDetailById(id);
        if (curVO == null) {
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "我的申请订单信息id");
        }
        return curVO;
    }

    @ApiOperation(value = "订单状态审核", notes = "提交资料、资料审核、待出函[重新开函]")
    @ApiImplicitParam(name = "id", value = "我的申请订单信息id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/applyOrder/auditStatus", method = RequestMethod.PUT)
    public Boolean applyOrderStatusOperate(@RequestBody ApplyOrderStatusOperateVO vo) {

        ApplyOrderInfoDO curDO = applyOrderInfoService.selectById(vo.getId());
        if (curDO == null) {
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "我的申请订单信息id");
        }
        if (curDO.getStatus() == null || curDO.getStatus().intValue() != vo.getStatus().intValue()) {
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "原我的申请订单状态与库表状态不一致");
        }
        ApplyOrderStatus status = ApplyOrderStatus.resolve(vo.getStatus().intValue());

        switch (status) {
            case WAIT_SUBMIT_DATA: {
                //待提交资料状态：提交资料
                if(StringUtils.isEmpty(vo.getReceiveAmtPic())){
                    throw new SystemRuntimeException(ErrorCode.ERROR, "到账证明文件不能为空");
                }
                return applyOrderInfoService.submitApplyData(vo);
            }
            case WAIT_APPROVAL_DATA: {
                //待审核状态：资料审核
                return applyOrderInfoService.confirmApplyData(vo);
            }
            case WAIT_OUT_GUARANTEE: {
                //待出函状态：可进行重新提交开函申请
                return applyOrderInfoService.reApplyGuarantee(vo);
            }
            default: {
                throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "暂不支持该状态[" + status.getStatus() + ":" + status.getDesc() + "]操作");
            }
        }
    }

    @RequestMapping(value = "/applyOrder/download", method = RequestMethod.POST)
    public void downloadApplyOrder(HttpServletResponse response, @RequestBody ListApplyOrderVO vo) {
        PageBo<ListApplyOrderDO> pageBo = applyOrderInfoService.selectPageBySql(1, DownloadExcelTool.maxRowNum, vo);
        List<ListApplyOrderDO> list = pageBo.getList().stream().map(l -> {
            l.setGuaranteeTypeName(GuaranteeType.getDescByType(l.getGuaranteeType()));
            l.setGuaranteeCategoryName(GuaranteeCategory.getDescByType(l.getGuaranteeCategory()));
            l.setStatusName(AcceptOrderStatus.getDescByStatus(l.getStatus()));
            l.setGuaranteeFeeYuan(StringUtil.toYuan(l.getGuaranteeFee()));
            l.setBidBondYuan(StringUtil.toYuan(l.getBidBond()));
            return l;
        }).collect(Collectors.toList());
        downloadExcelTool.downloadByTemplateName("applyOrderReport.xlsx", "我的申请订单", response, list, null);
    }



}
